package net.liteheaven.xblib.plugin;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

/**
 * 插件活动控制器
 *
 * 由插件实现者实现
 * 用me()和dad()方法实现对Activity的访问，替代this和super
 *
 * Created by LiangYu on 16/8/29
 */
public class XActivityController implements IXActivityImpl {

    private Activity mActivity;

    public XActivityController(Activity activity){
        this.mActivity = activity;
    }

    protected XProxyActivity me() {
        return (XProxyActivity) mActivity;
    }

    protected IXActivityImpl dad(){
        if (mActivity instanceof IHasDad){
            IHasDad hasDad = (IHasDad)mActivity;
            return hasDad.getDad();
        }
        return null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        dad().onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        dad().onDestroy();
    }

    @Override
    public void onResume() {
        dad().onResume();
    }

    @Override
    public void onPause() {
        dad().onPause();
    }

    @Override
    public void onStart() {
        dad().onStart();
    }

    @Override
    public void onStop() {
        dad().onStop();
    }

    @Override
    public void onRestart() {
        dad().onRestart();
    }

    @Override
    public void finish() {
        dad().finish();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return dad().dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return dad().onTouchEvent(event);
    }

    @Override
    public void onBackPressed() {
        dad().onBackPressed();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        dad().onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onNewIntent(Intent intent) {
        dad().onNewIntent(intent);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        dad().onSaveInstanceState(outState);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        dad().onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        dad().onWindowFocusChanged(hasFocus);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        dad().onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return dad().onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return dad().onKeyUp(keyCode, event);
    }

    @Override
    public void setContentView(View view) {
        dad().setContentView(view);
    }

    @Override
    public void setContentView(int layoutResID) {
        dad().setContentView(layoutResID);
    }

    @Override
    public View findViewById(int id) {
        return dad().findViewById(id);
    }

    @Override
    public void attachBaseContext(Context newBase) {
        dad().attachBaseContext(newBase);
    }
}
